/* jshint esversion: 6 */
$(document).ready(function() {
    ifLoged();
    //填充数据
    // $.getJSON("data/city.json", function(data) {
    //         //进行全局数据缓存
    //     })
    //实例化分页 请求数据函数对象 选择直接数据填充 不分页 并进行全局数据缓存
    var page = 1;
    fillData.init('.pageArray', url + 'v1/hotel/list', function(data) {
        fillHotelData(data.data);
    }, {
        cityName: "北京",
        limit: 16,
        page: 1,
    }, {
        ifPaging: true,
    });
    $.ajax(url + 'v1/city/list', {
            method: "get", // get请求
            dataType: 'json', // 进行json解析
            xhrFields: {
                withCredentials: true // 允许跨域名储存和访问cookie
            }
        })
        .done(function(data) { // 处理ajax成功的回调
            cityList = data.data;
            fillCityInfo(cityList);
        });
    //选择城市框获得焦点时加载城市信息
    $('#location').focus(function() {
        $('.citySelect').fadeIn(500);
    });
    $('#location').blur(function() {
        $('.citySelect').fadeOut(500);
    });
    setTimeout(() => {
        $('.citySelect').on('click', ".citylistitem", function() {
            $('#location').val(this.dataset.name);
        });
        $('.cityItem').on('mouseover', function() {
            $(this).addClass("active").siblings().removeClass("active");
            $(this).siblings().find('.cityMain').hide();
            $(this).find('.cityMain').show();
        });
        $('.city').first().addClass("active");
        $('.city').on('click', function() {
            let cityname = this.dataset.name;
            $(this).addClass("active").siblings().removeClass("active");
            fillData.init('.pageArray', url + 'v1/hotel/list', function(data) {
                fillHotelData(data.data);
            }, {
                cityName: cityname,
                limit: 16,
                page: page,
            }, {
                ifPaging: true,
            });
        });
    }, 1000);
    var startTime = "",
        endTime = "";
    laydate.render({
        elem: '#calendar', //指定元素
        range: '至',
        format: 'yyyy-MM-dd',
        min: 0, //
        max: 30, //30天后
        done: function(value, fromDate, endDate) {
            var strS = fromDate.year + '/' + fromDate.month + '/' + fromDate.date,
                strE = endDate.year + '/' + endDate.month + '/' + endDate.date,
                dateStart = new Date(strS),
                dateEnd = new Date(strE);
            console.log(strS);
            console.log(strE);
            startTime = dateStart.getTime();
            endTime = dateEnd.getTime();
            console.log(startTime);
            console.log(endTime);
        }
    });
    $('.searchBar').on('click', function() {
        var checkTime;
        if (startTime && endTime) {
            checkTime = startTime + ',' + endTime;
        } else {
            checkTime = "";
        }
        var params = {
            position: $('#location').val(),
            checkTime: checkTime,
            time: $('#calendar').val(),
            keyword: $('#keyword').val(),
        }
        var a = JSON.stringify(params),
            b = encodeURIComponent(a); // 先进行url编码，将中文字符转码
        window.location.href = "screening.html?" + b;
    });
});

function fillHotelData(dataHotel) {
    hotellist = dataHotel.hotelList;
    var hotelItem = "";
    for (let i = 0; i < hotellist.length; i++) {
        const e = hotellist[i];
        // pic = url_file + e.picture[0];
        pic = "images/hotel.jpg";
        hotelItem += '<a  href="hotelDetail.html?id=' + e._id + '" class="hotelItem pr" style="background:url(' + pic + ');background-size:cover"><li ></li>';
        hotelItem += '<div class="hotelInfo pa" ><h5>' + e.name + '</h5>';
        hotelItem += '<span class="price">￥' + e.price + '</span></div></li></a> ';
    }
    $('.mainPart').html(hotelItem);
}